<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_user extends Controller_Abstract {
	
	/**
	 * 客户端顶部广告
	 */
	function actionadminuser()
	{
		$append=[];
		if($_SESSION['acct']['id']!=1){
			$append = array("id"=>$_SESSION['acct']['id']);
		}
		$this->union_table = acct::meta();
		$this->lists($append);
	}
	function actionIndex()
	{
		$append = array("acct_id"=>$_SESSION['acct']['id']);
		$this->_view['viplevel_arr'] = viplevel::selectviplevel($_SESSION['acct']['id']);
		$company=company::find()->asArray()->getAll();
		$this->_view['company'] = $company;
		//获取搜索参数
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		$dis_param = new helper_disposeparam($this->param);
		$lastsql = $dis_param->getsql();//OUT PUT用
		$_SESSION['lastsql'] = $lastsql;//OUT PUT用

		$this->union_table = acctuser::meta();
		$this->lists($append);
	}
	/**
	 * 用户设置
	 */
	 
	function actionAdd() {
		//设置要操作的表单，并设置自动插入方式
		if($this->_context->isGET()){
	    	if(!empty($this->_context->id)){
	    		$user = acctuser::find('id=?',$this->_context->id)->getOne();
	    		$this->_view['info'] = $user;
	    	}

	    }
		if($this->_context->isPOST())
		{
			if(!empty($this->_context->id)){
				$user = acctuser::find('id=?',$this->_context->id)->getOne();
				$user->is_delete=intval($this->_context->is_delete);
				$user->is_comment=$this->_context->is_comment;
				$user->fx_grade=$this->_context->fx_grade;
				$user->viplevel_id=$this->_context->viplevel_id;
				$user->due_time=strtotime($this->_context->due_time);
				$user->real_name=$this->_context->real_name;
				$user->id_card_num=$this->_context->id_card_num;
		        $user->save(0,'update');
			}
			if(!$_POST['id'])	{
	            //Syslog::logs ( '添加视频,id :' . $user->id );
	        }else{
	        	
		        Syslog::logs ( '编辑用户,id :' . $user->id );
		    }
	        $this->_context->forwardUrl = url("user/index");
			$this->success('编辑成功');
		}
	}
	 
	/**
	 * 删除用户
	 */
	function actionDel() {
	      Syslog::logs("删除用户，ID：".print_r($this->_context->ids,true));
	      $this->union_table = Acctuser::meta();
	      $this->delete(array('id'=>$this->_context->ids));
	}
	//修改上级用户
	function actioneditparent(){
		$user_id = $this->_context->user_id;
		$user = Acctuser::find(array("id"=>$user_id,"acct_id"=>$_SESSION['acct']['id']))->getOne();
		if($this->_context->isGET ()){
			$this->_view['user_id']=$user_id;
			$this->_view['parent_id']=$user->parentid;
		}else{
			$user_id=$this->_context->user_id;
			$parent_id = $this->_context->parent_id;
			
			if(!$user->id()){
				$this->error("不存在用户");
			}
			if($parent_id!=0){
				$parent = Acctuser::find(array("id"=>$parent_id,"acct_id"=>$_SESSION['acct']['id']))->getOne();
				if(!$parent->id()){
					$this->error("指定的修改的用户不存在");
				}
			}
			
			$res = Acctuser::editparent($user_id,$parent_id);
			if($res){
				$this->_context->forwardUrl = url("user/index");
				$this->success("修改成功");
			}
			else{
				$this->error("修改失败");
			}
		}
		
	}

	function actionlearninfo(){
		$info=paper::getindexinfo($this->_context->user_id);
		$user_id=$this->_context->user_id;
		$my_video_num=count(videoorder::myclass($this->_context->user_id));

		//课后答题正确率
		$ringht_num=videoquestiondolog::find("user_id = ? and isright=1",$this->_context->user_id)->getAll();
		$question_num=videoquestiondolog::find("user_id = ?",$this->_context->user_id)->getAll();
		$rightlv=round(count($ringht_num)/count($question_num),2)*100;
		$avg_right_per = $rightlv;

		$userinfo = acctuser::find('id = ?', $user_id)->asArray()->getOne();
		$company_num=acctuser::find('company_id = ?', $userinfo['company_id'])->asArray()->getAll();
		$com_user=[];
		for($i=0;$i<count($company_num);$i++){
			$com_user[$i]=intval($company_num[$i]['id']);
		}
		$users_vnum=[];
		for($i1=0;$i1<count($com_user);$i1++){
			$edu_video_exam_sql = 'select * from edu_video_exam where user_id = ' . $com_user[$i1] .' group by chapter_id ';
			$edu_video_exam_data = QDB::getConn()->execute($edu_video_exam_sql)->fetchAll();
			$users_vnum_['num'] = count($edu_video_exam_data);
			$users_vnum_['id']=$com_user[$i1];
			array_push($users_vnum,$users_vnum_);
		}
		$last_names = array_column($users_vnum,'num');
		array_multisort($last_names,SORT_DESC,$users_vnum);
		$aaa=0;
		foreach ($users_vnum as $k=>$v){
			if($v['id']==intval($user_id)){
				$aaa= $k;
			}
		}
		$my_v_sort=intval($aaa)+1;//我的企业学习课程数排名
		$qinshang = intval($my_v_sort/count($company_num)*40)+60;//勤商

		$my_learn_num_='select distinct chapter_id from edu_video_exam where user_id = ' . $user_id ;
		$my_learn_num= QDB::getConn()->execute($my_learn_num_)->fetchAll();

		$this->_view['info']=$info;
		$this->_view['my_video_num']=$my_video_num;
		$this->_view['avg_right_per']=$avg_right_per;
		$this->_view['qinshang']=$qinshang;
		$this->_view['my_v_sort']=$my_v_sort;
		$this->_view['my_learn_num']=count($my_learn_num);
	}

	function actionexport() {
		$col = array(
			'A1' => "用户昵称",
			'B1' => "累计消费金额",
			'C1' => "可提现佣金",
			'D1' => "已提现佣金",
			'E1' => "待确认收货佣金",
			'F1' => "用户类型",
			'G1' => "vip等级",
			'H1' => "vip到期时间",
			'I1' => "手机号",
			'J1' => "身份证",
			'K1' => "真实姓名",
			'L1' => "累计学习天数",
			'M1' => "今日做题数",
			'N1' => "今日正确率",
			'O1' => "累计做题数",
			'P1' => "累计正确率",
			'Q1' => "购买课程数",
			'R1' => "课后答题正确率",
			'S1' => "勤商",
			'T1' => "企业学习课程数排名",
			'U1' => "学习课程数",
		);
		$sql = $_SESSION['lastsql'];
		$this->union_table = acctuser::meta();
		$res = call_user_func_array(array($this->union_table, 'find'), $sql)->asArray()->getAll();
		$data = array();
		for ($i = 0; $i < count($res); $i++) {
			$learn_info=paper::getindexinfo($res[$i]['id']);
			$video_num=count(videoorder::myclass($res[$i]['id']));
			if($res[$i]['viplevel_id']!=0){
				$viptitle = viplevel::find('id = ?',$res[$i]['viplevel_id'])->asArray()->getOne();
			}else{
				$viptitle['title'] = '不是会员';
			}

			$user_id=$res[$i]['id'];
			//课后答题正确率
			$ringht_num=videoquestiondolog::find("user_id = ? and isright=1",$user_id)->getAll();
			$question_num=videoquestiondolog::find("user_id = ?",$user_id)->getAll();
			if(count($question_num)!=0){
				$rightlv=round(count($ringht_num)/count($question_num),2)*100;
			}else{
				$rightlv=0;
			}

			$avg_right_per = $rightlv;

			$userinfo = acctuser::find('id = ?', $user_id)->asArray()->getOne();
			$company_num=acctuser::find('company_id = ?', $userinfo['company_id'])->asArray()->getAll();
			$com_user=[];
			for($i4=0;$i4<count($company_num);$i4++){
				$com_user[$i4]=intval($company_num[$i4]['id']);
			}
			$users_vnum=[];
			for($i1=0;$i1<count($com_user);$i1++){
				$edu_video_exam_sql = 'select * from edu_video_exam where user_id = ' . $com_user[$i1] .' group by chapter_id ';
				$edu_video_exam_data = QDB::getConn()->execute($edu_video_exam_sql)->fetchAll();
				$users_vnum_['num'] = count($edu_video_exam_data);
				$users_vnum_['id']=$com_user[$i1];
				array_push($users_vnum,$users_vnum_);
			}
			$last_names = array_column($users_vnum,'num');
			array_multisort($last_names,SORT_DESC,$users_vnum);
			$aaa=0;
			foreach ($users_vnum as $k=>$v){
				if($v['id']==intval($user_id)){
					$aaa= $k;
				}
			}
			$my_v_sort=intval($aaa)+1;//我的企业学习课程数排名
			$qinshang = intval($my_v_sort/count($company_num)*40)+60;//勤商

			$learn_num_='select distinct chapter_id from edu_video_exam where user_id = ' . $user_id ;//学习课程数
			$learn_num= QDB::getConn()->execute($learn_num_)->fetchAll();

			$data[$i] = array();
			$data[$i]['do_day'] = $learn_info['do_day'];//累计学习天数
			$data[$i]['today_dolog_num'] = $learn_info['today_dolog_num'];//今日做题数
			$data[$i]['today_avg_right_per'] = $learn_info['today_avg_right_per'].'%';//今日正确率
			$data[$i]['dolog_num'] = $learn_info['dolog_num'];//累计做题数
			$data[$i]['avg_right_per'] = $learn_info['avg_right_per'].'%';//累计正确率
			$data[$i]['my_video_num'] = $video_num;//购买课程数
			$data[$i]['avg_right_per'] = $avg_right_per.'%';
			$data[$i]['qinshang'] = $qinshang;
			$data[$i]['my_v_sort'] = $my_v_sort.'名';
			$data[$i]['learn_num'] = count($learn_num);

			$data[$i]['username'] = $res[$i]['username'];//用户昵称
			$data[$i]['total_consume'] = $res[$i]['total_consume'];//累计消费金额
			$data[$i]['commission'] = $res[$i]['commission'];//可提现佣金
			$data[$i]['used_commission'] = $res[$i]['used_commission'];//已提现佣金
			$data[$i]['wait_commission'] = $res[$i]['wait_commission'];//待确认收货佣金
			$res[$i]['type'] ==0?$data[$i]['type']='管理员':$data[$i]['type']='普通用户';//用户类型
			$data[$i]['viplevel_id'] = $viptitle['title'];//vip等级
			$data[$i]['due_time'] = date('Y-m-d H:i:s',$res[$i]['due_time']);//vip到期时间
			$data[$i]['phone'] = $res[$i]['phone'];//手机号
			$data[$i]['id_card_num'] = $res[$i]['id_card_num'];//身份证
			$data[$i]['real_name'] = $res[$i]['real_name'];//真实姓名
		}
		Helper_Paylogexcel::userinfoexecl($col, "用户信息", 20, $data, 20);
	}

}


